home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #47 (Aug 89) / PREC Code / PREC4.p < prev    next >
Text File  |  1989-06-14  |  11KB  |  256 lines

  1. {The Project should have the following files in it:     }
  2. {     µRunTime.lib     LSP This is for main Pascal runtime library}
  3. {     Interface.lib      LSP This is the Mac trap interfaces}
  4. {     InitTheMenus.p      This initializes the Menus.}
  5. {     About       Modal Dialog}
  6. {     Options       Window}
  7. {    HandleTheMenus         Handle the menu selections.}
  8. {Set  RUN OPTIONS to use the  resource file PREC4.RSRC  }
  9. { RMaker file to use is PREC4.R  }
  10. {   PREC4.p      Main program  }
  11. program PREC4;
  12. {Program name:  PREC4.p  }
  13. {Function:  This is the main module for this program.  }
  14. {History: 6/10/89 Original by Prototyper.   }
  15. { Created for MacTutor   }
  16. { ©1989 CopyRight MacTutor  All Rights Reserved   }
  17.  
  18.     uses
  19.         globals, About, Options, InitTheMenus, HandleTheMenus;
  20.     var                                 {Main variables}
  21.         myEvent: EventRecord;          {Event record for all events}
  22.         doneFlag: boolean;             {Exit program flag}
  23.         code: integer;                 {Determine event type}
  24.         whichWindow: WindowPtr;        {See which window for event}
  25.         tempRect, OldRect: Rect;        {Rect for dragging}
  26.         mResult: longint;              {Menu list and item selected values}
  27.         theMenu, theItem: integer;{Menu list and item selected}
  28.         chCode: integer;               {Key code}
  29.         ch: char;                      {Key pressed in Ascii}
  30.         theInput: TEHandle;              {Used in text edit selections}
  31.         myPt: Point;                     {Temp Point, used in Zoom}
  32.  
  33.  
  34. begin                               {Start of main body}
  35.  
  36.     MoreMasters;                    {This reserves space for more handles}
  37.     InitGraf(@thePort);             {Quickdraw Init}
  38.     InitFonts;                      {Font manager init}
  39.     InitWindows;                    {Window manager init}
  40.     InitMenus;                      {Menu manager init}
  41.     TEInit;                         {Text edit init}
  42.     InitDialogs(nil);               {Dialog manager}
  43.  
  44.     FlushEvents(everyEvent, 0);{Clear out all events}
  45.     InitCursor;                     {Make an arrow cursor}
  46.  
  47.     doneFlag := FALSE;              {Do not exit program yet}
  48.  
  49.     Init_My_Menus;                  {Initialize menu bar}
  50.  
  51.     theInput := nil;                  {Init to no text edit selection active}
  52.     Init_Options;                       {Initialize the window routines}
  53.     Open_Options(theInput);         {Open the window routines at program start}
  54.     D_About;                        {Open the modal dialog routines at program start}
  55.     reply.fname := '';
  56.     repeat                          {Start of main event loop}
  57.         if (theInput <> nil) then{See if a TE is active}
  58.             TEIdle(theInput);       {Blink the cursor if everything is ok}
  59.         SystemTask;                 {For support of desk accessories}
  60.  
  61.         if GetNextEvent(everyEvent, myEvent) then{If event then...}
  62.             begin                   {Start handling the event}
  63.                 code := FindWindow(myEvent.where, whichWindow);{Get which window the event happened in}
  64.  
  65.  
  66.                 case myEvent.what of{Decide type of event}
  67.                     MouseDown:         {Mouse button pressed}
  68.                         begin           {Handle the pressed button}
  69.                             if (code = inMenuBar) then{See if a menu selection}
  70.                                 begin{Get the menu selection and handle it}
  71.                                     mResult := MenuSelect(myEvent.Where);{Do menu selection}
  72.                                     theMenu := HiWord(mResult);{Get the menu list number}
  73.                                     theItem := LoWord(mResult);{Get the menu list item number}
  74.                                     Handle_My_Menu(doneFlag, theMenu, theItem, theInput);{Handle the menu}
  75.                                 end;{End of inMenuBar}
  76.  
  77.                             if (code = InDrag) then{See if in a window drag area}
  78.                                 begin{Do dragging the window}
  79.                                     tempRect := screenbits.bounds;{Get screen area,  l,t,r,b, drag area}
  80.                                     SetRect(tempRect, tempRect.Left + 10, tempRect.Top + 25, tempRect.Right - 10, tempRect.Bottom - 10);{}
  81.                                     DragWindow(whichWindow, myEvent.where, tempRect);{Drag the window}
  82.                                 end;{End of InDrag}
  83.  
  84.                             if ((code = inGrow) and (whichWindow <> nil)) then{In a grow area of the window}
  85.                                 begin{Handle the growing}
  86.                                     SetPort(whichWindow);{Get ready to draw in this window}
  87.  
  88.                                     myPt := myEvent.where;{Get mouse position}
  89.                                     GlobalToLocal(myPt);{Make it relative}
  90.  
  91.                                     OldRect := WhichWindow^.portRect;{Save the rect before resizing}
  92.  
  93.                                     with screenbits.bounds do{use the screens size}
  94.                                         SetRect(tempRect, 15, 15, (right - left), (bottom - top) - 20);{l,t,r,b}
  95.                                     mResult := GrowWindow(whichWindow, myEvent.where, tempRect);{Grow it}
  96.                                     SizeWindow(whichWindow, LoWord(mResult), HiWord(mResult), TRUE);{Resize to result}
  97.  
  98.  
  99.                                     SetPort(whichWindow);{Get ready to draw in this window}
  100.  
  101.                                     SetRect(tempRect, 0, myPt.v - 15, myPt.h + 15, myPt.v + 15); {Position for horz scrollbar area}
  102.                                     EraseRect(tempRect);{Erase old area}
  103.                                     InvalRect(tempRect);{Flag us to update it}
  104.                                     SetRect(tempRect, myPt.h - 15, 0, myPt.h + 15, myPt.v + 15);  {Position for vert scrollbar area}
  105.                                     EraseRect(tempRect);{Erase old area}
  106.                                     InvalRect(tempRect);{Flag us to update it}
  107.                                     DrawGrowIcon(whichWindow);{Draw the grow Icon again}
  108.                                 end;{End of doing the growing}
  109.  
  110.                             if (code = inZoomIn) or (code = inZoomOut) then{Handle Zooming windows}
  111.                                 begin{}
  112.                                     if (WhichWindow <> nil) then{See if we have a legal window}
  113.                                         begin{}
  114.                                             SetPort(whichWindow);{Get ready to draw in this window}
  115.  
  116.                                             myPt := myEvent.where;{Get mouse position}
  117.                                             GlobalToLocal(myPt);{Make it relative}
  118.  
  119.                                             OldRect := whichWindow^.portRect;{Save the rect before resizing}
  120.  
  121.                                             if TrackBox(whichWindow, myPt, code) then{Zoom it}
  122.                                                 begin{}
  123.                                                     ZoomWindow(WhichWindow, code, TRUE);{Resize to result}
  124.                                                     SetRect(tempRect, 0, 0, 32000, 32000);{l,t,r,b}
  125.                                                     EraseRect(tempRect);{Make sure we update the whole window effectively}
  126.                                                     InvalRect(tempRect);{Tell ourselves to update, redraw, the window contents}
  127.                                                 end;{}
  128.                                         end;{}
  129.                                 end;{}
  130.  
  131.                             if (code = inGoAway) then{See if in a window goaway area}
  132.                                 begin{Handle the goaway button}
  133.                                     if TrackGoAway(whichWindow, myEvent.where) then{See if mouse released in GoAway box}
  134.                                         begin{Handle the GoAway}
  135.                                             case (GetWRefCon(whichWindow)) of{Do the appropiate window}
  136.                                                 1: 
  137.                                                     begin
  138.                                                         Close_Options(whichWindow, theInput);{Close this window}
  139.                                                         doneFlag := TRUE;
  140.                                                     end;
  141.                                                 otherwise{Handle others dialogs}
  142.                                                     begin{Others}
  143.                                                     end;{End of the otherwise}
  144.                                             end;{End of the case}
  145.                                         end;{End of TrackGoAway}
  146.                                 end;{End of InGoAway}
  147.  
  148.                             if (code = inContent) then{See if in a window}
  149.                                 begin{Handle the hit inside a window}
  150.                                     if (whichWindow <> FrontWindow) then{See if already selected or not, in front if selected}
  151.                                         SelectWindow(whichWindow){Select this window to make it active}
  152.                                     else{If already in front the already selected}
  153.                                         begin{Handle the button in the content}
  154.                                             SetPort(whichWindow);{Get ready to draw in this window}
  155.                                             case (GetWRefCon(whichWindow)) of{Do the appropiate window}
  156.                                                 1: 
  157.                                                     Do_Options(myEvent, theInput);{Handle this window}
  158.                                                 otherwise{Handle others dialogs}
  159.                                                     begin{Others}
  160.                                                     end;{End of the otherwise}
  161.                                             end;{End of the case}
  162.                                         end;{End of else}
  163.                                 end;{End of inContent}
  164.  
  165.                             if (code = inSysWindow) then{See if a DA selection}
  166.                                 SystemClick(myEvent, whichWindow);{Let other programs in}
  167.  
  168.                         end;            {End of MouseDown}
  169.  
  170.                     KeyDown, AutoKey:{Handle key inputs}
  171.                         begin           {Get the key and handle it}
  172.                             with myevent do{Check for menu command keys}
  173.                                 begin{}
  174.                                     chCode := BitAnd(message, CharCodeMask);{Get character}
  175.                                     ch := CHR(chCode);{Change to ASCII}
  176.                                     if (Odd(modifiers div CmdKey)) then{See if Command key is down}
  177.                                         begin{}
  178.                                             mResult := MenuKey(ch);{See if menu selection}
  179.                                             theMenu := HiWord(mResult);{Get the menu list number}
  180.                                             theItem := LoWord(mResult);{Get the menu item number}
  181.                                             if (theMenu <> 0) then{See if a list was selected}
  182.                                                 Handle_My_Menu(doneFlag, theMenu, theItem, theInput);{Do the menu selection}
  183.                                             if ((ch = 'x') or (ch = 'X')) and (theInput <> nil) then{}
  184.                                                 TECut(theInput);{Handle a Cut in a TE area}
  185.                                             if ((ch = 'c') or (ch = 'C')) and (theInput <> nil) then{}
  186.                                                 TECopy(theInput);{Handle a Copy in a TE area}
  187.                                             if ((ch = 'v') or (ch = 'V')) and (theInput <> nil) then{}
  188.                                                 TEPaste(theInput);{Handle a Paste in a TE area}
  189.                                         end{}
  190.                                     else if (theInput <> nil) then{}
  191.                                         begin
  192.                                             if ch = chr(9) then
  193.                                                 begin
  194.                                                     TEDeactivate(theInput);{Yes, so turn it off}
  195.                                                     if theInput = TitleFieldhndl then
  196.                                                         theInput := WidthFieldhndl
  197.                                                     else if theInput = WidthFieldhndl then
  198.                                                         theInput := HeightFieldhndl
  199.                                                     else if theInput = HeightFieldhndl then
  200.                                                         theInput := TitleFieldhndl;
  201.                                                     TEActivate(theInput);{Turn it on}
  202.                                                     recordstatus;
  203.                                                 end
  204.                                             else
  205.                                                 TEKey(ch, theInput);{}
  206.                                         end;
  207.                                 end;{}
  208.                         end;            {End for KeyDown,AutoKey}
  209.  
  210.                     UpDateEvt:         {Update event for a window}
  211.                         begin           {Handle the update}
  212.                             whichWindow := WindowPtr(myEvent.message);{Get the window the update is for}
  213.                             BeginUpdate(whichWindow);{Set the clipping to the update area}
  214.                             case (GetWRefCon(whichWindow)) of{Do the appropiate window}
  215.                                 1: 
  216.                                     Update_Options(whichWindow);{Update this window}
  217.                                 otherwise           {Handle others dialogs}
  218.                                     begin           {Others}
  219.                                     end;            {End of the otherwise}
  220.                             end;                    {End of the case}
  221.                             EndUpdate(whichWindow);{Return to normal clipping area}
  222.                         end;            {End of UpDateEvt}
  223.  
  224.                     DiskEvt:           {Disk inserted event}
  225.                         begin           {Handle a disk event}
  226.                             if (HiWord(myevent.message) <> noErr) then{See if a diskette mount error}
  227.                                 begin{due to unformatted diskette inserted}
  228.                                     myEvent.where.h := ((screenbits.bounds.Right - screenbits.bounds.Left) div 2) - (304 div 2);{Center horz}
  229.                                     myEvent.where.v := ((screenbits.bounds.Bottom - screenbits.bounds.Top) div 3) - (104 div 2);{Top 3ed vertically}
  230.                                     InitCursor;{Make sure it has an arrow cursor}
  231.                                     chCode := DIBadMount(myEvent.where, myevent.message);{Let the OS handle the diskette}
  232.                                 end;{}
  233.                         end;           {End of DiskEvt}
  234.  
  235.                     app1Evt:            {Check for events generated by this program}
  236.                         begin           {Start handling our events}
  237.                             if (HiWord(myEvent.message) = 1) and (LoWord(myEvent.Message) = 1) then{See if OPEN event for this window ID}
  238.                                 Open_Options(theInput);{Open the window}
  239.                             if (HiWord(myEvent.message) = 2) and (LoWord(myEvent.Message) = 1) then{See if CLOSE event for this window ID}
  240.                                 Close_Options(WindowPtr(ord4(-1)), theInput);{Close the window}
  241.                         end;            {End handling our events}
  242.  
  243.                     ActivateEvt:       {Window activated event}
  244.                         begin           {Handle the activation}
  245.                             whichWindow := WindowPtr(myevent.message);{Get the window to be activated}
  246.                             if odd(myEvent.modifiers) then{Make sure it is Activate and not DeActivate}
  247.                                 begin{Handle the activate}
  248.                                     SelectWindow(whichWindow);{Activate the window by selecting it}
  249.                                 end;{End of Activate}
  250.                         end;            {End of ActivateEvt}
  251.                     otherwise       {Used for debugging, to see what other events are coming in}
  252.                 end;                    {End of case}
  253.             end;                    {end of GetNextEvent}
  254.     until doneFlag;         {End of the event loop}
  255.  
  256. end.                                    {End of the program}